
set(EXOSIP_ROOT "" CACHE PATH "The root directory of exosip source package, cmake will automatically find exosip at system installation directory when left blank, you can manual setting this by -DEXOSIP_ROOT=<path>")

#[=======================================================================[
## Bracket Comment begin from cmake version 3.0 and later, 
## reference: https://cmake.org/cmake/help/v3.5/manual/cmake-language.7.html#bracket-argument
#]=======================================================================]

option(OPENSSL_ENABLE "Link with openssl? If you would use sip upon tls or dtls, enable this!" OFF)
if(OPENSSL_ENABLE)
  set(OPENSSL_INSTALL_DIR "" CACHE PATH "The root directory of an OpenSSL installation")
  if(NOT OPENSSL_INSTALL_DIR)
    message(STATUS "cmake will automatically find openssl at system installation directory when left blank"
                   ", you can manual setting this by -DOPENSSL_INSTALL_DIR=<path>")
  endif()
endif()

## all source files
file(GLOB HEADERS *.h *.hxx)
file(GLOB SOURCES *.c *.cxx *.cpp)

add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES})

if(MSVC)
  target_compile_definitions(${PROJECT_NAME} PRIVATE "_CRT_SECURE_NO_WARNINGS")
else()
  target_compile_options(${PROJECT_NAME} PRIVATE "-Wno-deprecated-declarations")
endif()

## link with rutil
target_include_directories(${PROJECT_NAME} PRIVATE "./rutil")
target_link_libraries(${PROJECT_NAME} PRIVATE rutil)

## find the exosip(including osip and osipparser)
find_package(Exosip REQUIRED)
if(Exosip_FOUND)
  message(STATUS "Found exosip:")
  message(STATUS "  Exosip_INCLUDE_DIRS=${Exosip_INCLUDE_DIRS}")
  message(STATUS "  Exosip_LIBRARIES=${Exosip_LIBRARIES}")
  
  target_include_directories(${PROJECT_NAME} PRIVATE ${Exosip_INCLUDE_DIRS})
  target_link_libraries(${PROJECT_NAME} PRIVATE ${Exosip_LIBRARIES})
endif()

#[[ ###### find the openssl and link with it. 
!!### It must be linked before the exosip library in the order of cmake, otherwise a lot of undefined errors will be prompted
######]]
if(OPENSSL_ENABLE)
  if(OPENSSL_INSTALL_DIR)
    set(OPENSSL_ROOT_DIR "${OPENSSL_INSTALL_DIR}")
  endif()
  set(OPENSSL_USE_STATIC_LIBS TRUE)
  #set(OPENSSL_MSVC_STATIC_RT TRUE)
  find_package(OpenSSL REQUIRED COMPONENTS SSL Crypto)
  if(OPENSSL_FOUND)
    message(STATUS "Found openssl ${OPENSSL_VERSION}")
    message(STATUS "  OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}")
    message(STATUS "  OPENSSL_LIBRARIES=${OPENSSL_LIBRARIES}")
    
    target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
    if(WIN32)
      target_compile_definitions(${PROJECT_NAME} PRIVATE ENABLE_OPENSSL) # for VS complier
      target_link_libraries(${PROJECT_NAME} PRIVATE Crypt32)
    endif()
  endif()
endif()

## find the spdlog libraries
find_package(spdlog 1.9.2 REQUIRED)
if(spdlog_FOUND)
  message(STATUS "Found the spdlog, version:${spdlog_VERSION}")
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE zkbfcp zklog)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

## Other dependent libraries
if(MSVC)
    target_link_libraries(${PROJECT_NAME} PRIVATE Dnsapi ws2_32 #[[Iphlpapi Qwave delayimp ##]])
elseif(UNIX)
    target_link_libraries(${PROJECT_NAME} PRIVATE pthread resolv rt dl)
endif()

## copy sdp files to ../bin/
add_custom_command(
  TARGET ${PROJECT_NAME} POST_BUILD
  COMMAND
    ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/samples/sdp.txt" "${EXECUTABLE_OUTPUT_PATH}" 
  COMMAND
    ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/samples/sdp-answer.txt" "${EXECUTABLE_OUTPUT_PATH}" 
  COMMENT
    "Copy runtime files to ${EXECUTABLE_OUTPUT_PATH}"
  VERBATIM
)

add_subdirectory(rutil)
add_subdirectory(zklog)
add_subdirectory(zkbfcp)